package executor;

import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author tongchen
 * @create 2023-01-25 9:59
 */
public class MyThreadPool1 {
    //创建阻塞队列组织任务
   LinkedBlockingQueue<Runnable> blockingQueue=new LinkedBlockingQueue<>();
   //创建线程类
    static   class MyThreads extends Thread{
        private LinkedBlockingQueue<Runnable> blockingQueue=null;
       @Override
       public void run() {
           //描述任务
           try {
               Runnable take = blockingQueue.take();
               //执行任务
               take.run();
           } catch (InterruptedException e) {
               throw new RuntimeException(e);
           }
       }
       private MyThreads(LinkedBlockingQueue queue){
           this.blockingQueue=queue;
       }
   }
    //创建队列组织线程
    List<MyThreads> list=new ArrayList<>();
   //构造方法实现任务的取出
    public MyThreadPool1(int num){
        for (int i=0;i<num;++i){
            MyThreads myThreads = new MyThreads(blockingQueue);
            //执行线程
            myThreads.start();
            //将线程加入队列
            list.add(myThreads);
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        blockingQueue.put(runnable);
    }

}
